import time
from cuslog import CusLog

version_info = (0, 1, 0)
versionstr = '.'.join(map(str, version_info))

runtimelogins = CusLog(log_type='runtimecalc')

def decorator(func):
	def wrapper(*args, **kwargs):
		runtimelogins.cusinfo(f"Starting execution of function '{func.__name__}'...")
		start_time = time.time()
		result = func(*args, **kwargs)
		end_time = time.time()

		#execduration = str(round(end_time - start_time, 2))
		execduration = float(round(end_time - start_time, 2))

		if(execduration > 60.0):
			execduration = float(round(execduration / 60.0, 2))
			if(execduration > 60.0):
				execdurationstr = str(round(execduration / 60.0, 2)) + " hours"
			else:
				execdurationstr = str(execduration) + " minutes"
		else:
			execdurationstr = str(execduration) + " seconds"

		runtimelogins.cusinfo(f"Function '{func.__name__}' executed in {execdurationstr}")
		#print(runtimelog.__dir__)
		#print(f"Function '{func.__name__}' executed in {execduration} seconds.")

		return result
	return wrapper

#def record(self, *args, **kwargs):
def runtimelog(loginstance):
	def decorator(func):
		def wrapper(*args, **kwargs):
			start_time = time.time()
			result = func(*args, **kwargs)
			end_time = time.time()

			execduration = str(round(end_time - start_time, 2))

			#loginstance.cusinfo(f"Function '{func.__name__}' executed in {execduration} seconds.")
			print(f"Function '{func.__name__}' executed in {execduration} seconds.")

			return result
		return wrapper
	return decorator

if __name__ == '__main__':
	import sys
	import os
	if (sys.argv[1] == '-v'):
		scriptname = os.path.basename(__file__)
		print(f"{scriptname}:{versionstr}")
		sys.exit(0)